﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.WVB.Framework;
using System.Windows.Data;
using Microsoft.WVB.Silverlight.Infrastructure;

namespace Microsoft.WVB.UI
{
    [TemplatePart(Name = MediaProgramInfo.RatingElementName, Type = typeof(Slider))]
    [TemplateVisualState(GroupName = VisualStates.GroupCommon, Name = VisualStates.StateNormal),
    TemplateVisualState(GroupName = VisualStates.GroupCommon, Name = VisualStates.StateBuffer)]
    public class MediaProgramInfo : Control
    {
        private const string RatingElementName = "RatingElement";
        internal Slider SliderControl { get; set; }

        public Double Rating
        {
            get { return (Double)GetValue(RatingProperty); }
            set { SetValue(RatingProperty, value); }
        }
        public static readonly DependencyProperty RatingProperty = null;

        public Boolean Buffering
        {
            get { return (Boolean)GetValue(BufferingProperty); }
            set { SetValue(BufferingProperty, value); }
        }
        public static readonly DependencyProperty BufferingProperty = null;

        public ProgramModel ViewModel { get; set; }

        static MediaProgramInfo()
        {
            BufferingProperty = DependencyProperty.Register("Buffering", typeof(Boolean), typeof(MediaProgramInfo), new PropertyMetadata(new PropertyChangedCallback(OnBufferingChanged)));
            RatingProperty = DependencyProperty.Register("Rating", typeof(Double), typeof(MediaProgramInfo), new PropertyMetadata(new PropertyChangedCallback(OnRatingChanged)));
        }

        static void OnBufferingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MediaProgramInfo)d).OnBufferingChanged((Boolean) e.OldValue, (Boolean)e.NewValue);
        }

        static void OnRatingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MediaProgramInfo)d).OnRatingChanged((Double)e.NewValue);
        }

        protected virtual void OnBufferingChanged(Boolean oldValue, Boolean newValue)
        {
            UpdateVisualState();
        }

        protected virtual void OnRatingChanged(Double newRating)
        {
            // TODO :ViewModel.UpdateRating(newRating);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
            SliderControl = base.GetTemplateChild(MediaProgramInfo.RatingElementName) as Slider;

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                if (SliderControl != null)
                {
                    SliderControl.Maximum = 5.0;
                    SliderControl.Minimum = 0.0;

                    Binding b = new Binding("Rating");
                    b.Source = this.ViewModel;
                    b.Mode = BindingMode.TwoWay;
                    SliderControl.SetBinding(Slider.ValueProperty, b);
                }
            }
        }

        public MediaProgramInfo()
        {
            this.DefaultStyleKey = typeof(MediaProgramInfo);

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                ViewModel = IoC.GetCreate<ProgramModel>();
                this.DataContext = ViewModel;
            }
        }

        internal void UpdateVisualState()
        {
            this.ChangeVisualState(true);
        }

        private void ChangeVisualState(bool useTransitions)
        {
            if (this.Buffering)
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateBuffer });
            }
            else
            {
                VisualStates.GoToState(this, useTransitions, new String[] { VisualStates.StateNormal });
            }
        }
    }
}
